//
//  UserOperationView.m
//  itboye_rjy_ios
//
//  Created by pro on 16/1/7.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "UserOperationView.h"

#import "GetCaptchaButton.h"

@interface UserOperationView ()

@property (nonatomic, assign) NSInteger line; // 行数
@property (nonatomic, assign) ZKUserViewType type; // 类型
@property (nonatomic, strong) NSArray *imgAry; // 图片数组
@property (nonatomic, strong) NSArray *titlesAry; // 标题数组

@property (nonatomic, strong) GetCaptchaButton *getCaptchButton; // 获取验证码按钮

@end

@implementation UserOperationView

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titlesAry type:(ZKUserViewType)type {
    
    if (self = [super initWithFrame:frame]) {
        
        _titlesAry = titlesAry; // placeholder
        
        _type = type; // 类型

        self.width = frame.size.width;
        self.height = frame.size.height;
        
        [self _initViews]; // 创建视图
    }
    
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {
    CGFloat x;
    CGFloat y;
    CGFloat width;
    CGFloat height;
    
    // 循环创建输入框视图
    for (int i = 0; i < _titlesAry.count; i ++) {
        
        x = 10;
        y = lineHeight * i;
        width = self.width - x * 2;
        height = lineHeight;
        
        UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
        tf.backgroundColor = [UIColor clearColor];
        tf.textColor = [UIColor darkGrayColor];
        
        // 涉及输入密码的输入框要用安全输入模式
        NSString * title = _titlesAry[i];
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
            if (title  != nil && [title containsString:@"密码"]) {
                tf.enabled = false;//iOS6.1Bug fix
                tf.secureTextEntry = true;
                tf.enabled = true;
                
            }
        }else{
            if ([title rangeOfString:@"密码"].location != NSNotFound) {
                tf.enabled = false;//iOS6.1Bug fix
                tf.secureTextEntry = true;
                tf.enabled = true;
            }
        }
        
        NSMutableAttributedString *placeholdAttr = [[NSMutableAttributedString alloc] initWithString:_titlesAry[i]];
        [placeholdAttr addAttribute:NSFontAttributeName value:FONT(15) range:NSMakeRange(0, placeholdAttr.length)];

        [tf setAttributedPlaceholder:placeholdAttr];
        [self addSubview:tf];
        
        tf.tag = 10 + i; // 用于取数据
    }
    
    // 循环创建分割线
    for (int i = 0; i < _titlesAry.count + 1; i ++) {
        
        x = 0;
        y = i * lineHeight;
        width = self.width;
        height = 1;
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        [self addSubview:line];
    }
    
    
    
    // 只要类型不是登陆，修改邮箱，修改密码就需要验证码，第二栏需要一个获得验证码的按钮
    if (_type != ZKUserViewLogin ) {
        x = 0;
        width = 80;
        height = 30;
        y = lineHeight + (lineHeight - height) / 2.0;

        _getCaptchButton = [[GetCaptchaButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
        _getCaptchButton.right = self.width - 10;
        // 单击
        [_getCaptchButton addTarget:self action:@selector(_getCaptcha) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_getCaptchButton];
        if (_type == ZKUserViewChangeMobile) {
            _getCaptchButton.hidden = YES;
        }else if (_type == ZKUserViewChangeEmail){
            _getCaptchButton.hidden = YES;
        }
    }
    
    // 只有类型是手机号需要验证码，第三栏需要一个获得验证码的按钮
    if (_type == ZKUserViewChangeMobile) {
        x = 0;
        width = 100;
        height = 25;
        y = lineHeight * 2 + (lineHeight - height) / 2.0;
        
        _getCaptchButton = [[GetCaptchaButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
        _getCaptchButton.right = self.width - 10;
        // 单击
        [_getCaptchButton addTarget:self action:@selector(_getCaptcha) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_getCaptchButton];
        _getCaptchButton.backgroundColor = SS_NAVI_BLUE_COLOR;
        _getCaptchButton.captchaLabel.textColor = [UIColor whiteColor];
        _getCaptchButton.layer.borderColor = SS_NAVI_BLUE_COLOR.CGColor;
    }

    
    
}

#pragma mark - 按钮点击处理
// 获取验证码按钮点击
- (void)_getCaptcha {
    
    UITextField *mobileTF;
    if (_type != ZKUserViewChangeMobile) {
        mobileTF = (UITextField *)[self viewWithTag:10];
        if ([mobileTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"请输入手机号！" withDuration:1];
            return;
        }
        else if (mobileTF.text.length != 11) {
            [SVProgressHUD showOnlyStatus:@"手机号输入不正确！" withDuration:1];
            return;
        }

    }else{
        mobileTF = (UITextField *)[self viewWithTag:11];
        if ([mobileTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"请输入手机号！" withDuration:1];
            return;
        }
        else if (mobileTF.text.length != 11) {
            [SVProgressHUD showOnlyStatus:@"手机号输入不正确！" withDuration:1];
            return;
        }

    }
    
    // 请求注册验证码
    if (_type == ZKUserViewRegister) {
        [_getCaptchButton getCaptchaWithMobile:mobileTF.text type:CAPTCHA_REGISTER captchaBlock:^(NSString *captcha) {
//            UITextField *tf = (UITextField *)[self viewWithTag:11];
//            tf.text = captcha;
        }];
    } else if (_type == ZKUserViewForgetPwd) {
        [_getCaptchButton getCaptchaWithMobile:mobileTF.text type:CAPTCHA_UPDATE_PSW captchaBlock:^(NSString *captcha) {
//            UITextField *tf = (UITextField *)[self viewWithTag:11];
//            tf.text = captcha;
        }];
    }
    
    else if (_type == ZKUserViewMsgLogin){
        [_getCaptchButton getCaptchaWithMobile:mobileTF.text type:CAPTCHA_LOGIN captchaBlock:^(NSString *captcha) {
//            UITextField *tf = (UITextField *)[self viewWithTag:11];
//            tf.text = captcha;
        }];
        
    }


    else if (_type == ZKUserViewChangeMobile){
        [_getCaptchButton getCaptchaWithMobile:mobileTF.text type:CAPTCHA_CHANGE_NEW_PHONE captchaBlock:^(NSString *captcha) {
            UITextField *tf = (UITextField *)[self viewWithTag:12];
            tf.text = captcha;
        }];

    }
    
}

// 点击注册时，获得手机，验证码，密码
- (void)getRegistrtInfo:(void(^)(NSDictionary *infoDic))infoBlock {
    UITextField *mobileTF = (UITextField *)[self viewWithTag:10];
    UITextField *codeTF = (UITextField *)[self viewWithTag:11];
    UITextField *passwordTF = (UITextField *)[self viewWithTag:12];
    UITextField *invitationCodeTF = (UITextField *)[self viewWithTag:13];
    // 数据填写不正确
    if (![self _isInputOk]) {
        return;
    } else {
        NSDictionary *infoDic = @{@"mobile":mobileTF.text,
                                  @"code":codeTF.text,
                                  @"password":passwordTF.text,
                                  @"invite_id":invitationCodeTF.text
                                  
                                  };
        if (infoBlock) {
            infoBlock(infoDic);
        }
    }
}
// 点击登录时，获得手机，密码
- (void)getLoginInfo:(void(^)(NSDictionary *infoDic))infoBlock {
    UITextField *mobileTF = (UITextField *)[self viewWithTag:10];
    UITextField *passwordTF = (UITextField *)[self viewWithTag:11];
    // 数据填写不正确
    if (![self _isInputOk]) {
        return;
    } else {
        NSDictionary *infoDic = @{@"mobile":mobileTF.text,
                                  @"password":passwordTF.text};
        if (infoBlock) {
            infoBlock(infoDic);
        }
    }
}
// 点击短信登陆登录时，获得手机，密码
- (void)getMesLoginInfo:(void(^)(NSDictionary *infoDic))infoBlock {
    UITextField *mobileTF = (UITextField *)[self viewWithTag:10];
    UITextField *codeTF = (UITextField *)[self viewWithTag:11];
    // 数据填写不正确
    if (![self _isInputOk]) {
        return;
    } else {
        NSDictionary *infoDic = @{@"mobile":mobileTF.text,
                                  @"code":codeTF.text};
        if (infoBlock) {
            infoBlock(infoDic);
        }
    }
}
// 点击重置密码时，获得手机，验证码，密码
- (void)getForgetPwdInfo:(void(^)(NSDictionary *infoDic))infoBlock {
    UITextField *passwordTF = (UITextField *)[self viewWithTag:12];
    UITextField *mobileTF = (UITextField *)[self viewWithTag:10];
    UITextField *codeTF = (UITextField *)[self viewWithTag:11];
    // 数据填写不正确
    if (![self _isInputOk]) {
        return;
    } else {
        NSDictionary *infoDic = @{@"mobile":mobileTF.text,
                                  @"code":codeTF.text,
                                  @"password":passwordTF.text};
        if (infoBlock) {
            infoBlock(infoDic);
        }
    }
}

// 点击修改邮箱时，获得邮箱，密码
- (void)getChangeEmailInfo:(void(^)(NSDictionary *infoDic))infoBlock{
     UITextField *passwordTF = (UITextField *)[self viewWithTag:10];
     UITextField *emailTF = (UITextField *)[self viewWithTag:11];
    // 数据填写不正确
    if (![self _isInputOk]) {
        return;
    } else {
        NSDictionary *infoDic = @{@"email":emailTF.text,
                                  @"password":passwordTF.text};
        if (infoBlock) {
            infoBlock(infoDic);
        }
    }



}

// 点击修改手机号时，获得手机号， 密码， 验证码
- (void)getChangeMobileInfo:(void(^)(NSDictionary *infoDic))infoBlock{
    UITextField *passwordTF = (UITextField *)[self viewWithTag:10];
    UITextField *mobileTF = (UITextField *)[self viewWithTag:11];
    UITextField *codeTF = (UITextField *)[self viewWithTag:12];
        // 数据填写不正确
    if (![self _isInputOk]) {
        return;
    } else {
        NSDictionary *infoDic = @{@"mobile":mobileTF.text,
                                  @"code":codeTF.text,
                                  @"password":passwordTF.text};
        if (infoBlock) {
            infoBlock(infoDic);
        }
    }

}




// 是否填写正确
- (BOOL)_isInputOk {
    if (_type == ZKUserViewLogin) {
        UITextField *mobileTF = (UITextField *)[self viewWithTag:10];
        UITextField *passwordTF = (UITextField *)[self viewWithTag:11];
        if ([mobileTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"请输入手机号！" withDuration:1];
            return NO;
        } else if (mobileTF.text.length != 11) {
            [SVProgressHUD showOnlyStatus:@"手机号输入不正确！" withDuration:1];
            return NO;
        } else if ([passwordTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"未填写密码" withDuration:1];
            return NO;
        }
    } else if (_type == ZKUserViewRegister) {
        UITextField *mobileTF = (UITextField *)[self viewWithTag:10];
        UITextField *codeTF = (UITextField *)[self viewWithTag:11];
        UITextField *passwordTF = (UITextField *)[self viewWithTag:12];

        if ([mobileTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"请输入手机号！" withDuration:1];
            return NO;
        } else if (mobileTF.text.length != 11) {
            [SVProgressHUD showOnlyStatus:@"手机号输入不正确！" withDuration:1];
            return NO;
        } else if ([codeTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"未填写验证码" withDuration:1];
            return NO;
        } else if ([passwordTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"未填写密码" withDuration:1];
            return NO;
        }
    } else if (_type == ZKUserViewForgetPwd) {
        UITextField *mobileTF = (UITextField *)[self viewWithTag:10];
        UITextField *codeTF = (UITextField *)[self viewWithTag:11];
        UITextField *newPasswordTF = (UITextField *)[self viewWithTag:12];
        UITextField *confirmNewPasswordTF = (UITextField *)[self viewWithTag:13];

        if ([mobileTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"请输入手机号！" withDuration:1];
            return NO;
        } else if (mobileTF.text.length != 11) {
            [SVProgressHUD showOnlyStatus:@"手机号输入不正确！" withDuration:1];
            return NO;
        } else if ([codeTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"未填写验证码" withDuration:1];
            return NO;
        } else if ([newPasswordTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"未填写旧密码" withDuration:1];
            return NO;
        } else if ([confirmNewPasswordTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"未填写新密码" withDuration:1];
            return NO;
        } else if (![confirmNewPasswordTF.text isEqualToString:newPasswordTF.text]) {
            [SVProgressHUD showOnlyStatus:@"两次密码输入不相同" withDuration:1];
            return NO;
        }
    } else if (_type == ZKUserViewMsgLogin) {
        UITextField *mobileTF = (UITextField *)[self viewWithTag:10];
        UITextField *codeTF = (UITextField *)[self viewWithTag:11];
        
        if ([mobileTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"请输入手机号！" withDuration:1];
            return NO;
        } else if (mobileTF.text.length != 11) {
            [SVProgressHUD showOnlyStatus:@"手机号输入不正确！" withDuration:1];
            return NO;
        } else if ([codeTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"未填写验证码" withDuration:1];
            return NO;
        }
    }
    
    
    else if (_type == ZKUserViewChangeMobile ){
        UITextField *passwordTF = (UITextField *)[self viewWithTag:10];
        UITextField *mobileTF = (UITextField *)[self viewWithTag:11];
        UITextField *codeTF = (UITextField *)[self viewWithTag:12];
        
        if ([passwordTF.text isStringNull]){
            UserModel *user = [CommonCache getUserInfo];
            if ([user.reg_from isEqualToString:@"2"]){
                 [SVProgressHUD showOnlyStatus:@"请输入原手机号" withDuration:1];
            }else{
                [SVProgressHUD showOnlyStatus:@"请输入密码！" withDuration:1];

            }
            
            return NO;
        }

       else if ([mobileTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"请输入手机号！" withDuration:1];
            return NO;
        } else if (mobileTF.text.length != 11) {
            [SVProgressHUD showOnlyStatus:@"手机号输入不正确！" withDuration:1];
            return NO;
        } else if ([codeTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"未填写验证码" withDuration:1];
            return NO;
        }
    }
    
    
    else if (_type == ZKUserViewChangeEmail){
        UITextField *passwordTF = (UITextField *)[self viewWithTag:10];
        UITextField *emailTF = (UITextField *)[self viewWithTag:10];
        
        if ([emailTF.text isStringNull]) {
            [SVProgressHUD showOnlyStatus:@"请输入手机号！" withDuration:1];
            return NO;
        }else if ([passwordTF.text isStringNull]){
            [SVProgressHUD showOnlyStatus:@"请输入密码！" withDuration:1];
            return NO;
        }
        
        
    }
    
    
    
    return YES;
}
// 是否所有输入框都已经编辑了
- (BOOL)isAllTextFieldEdited {
    // 注册时，邀请码非必填
    if (_type == ZKUserViewRegister) {
        for (int i = 0; i < _titlesAry.count - 1; i ++) {
            UITextField *tf = (UITextField *)[self viewWithTag:10 + i];
            if ([tf.text isStringNull]) {
                return NO;
            }
        }
    } else {
        for (int i = 0; i < _titlesAry.count; i ++) {
            UITextField *tf = (UITextField *)[self viewWithTag:10 + i];
            if ([tf.text isStringNull]) {
                return NO;
            }
        }
    }
    
    
    
    return YES;
}

@end
